home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Papers / Rentzsch / Code / Align / Aligned.h < prev   
Encoding:
C/C++ Source or Header  |  2001-06-23  |  4.3 KB  |  139 lines

  1. /****************************************************************************************
  2.     Aligned.h
  3.         <http://redshed.net/align>
  4.  
  5.         The buzzard told the monkey "You are chokin' me
  6.             "Release your hold and I will set you free".
  7.         The monkey looked the buzzard right dead in the eye
  8.             And said, "Your story's so touching, it sounds just like a lie".
  9.     
  10.     Copyright © 1999-2001 Red Shed Software. All rights reserved.
  11.     by Jonathan 'Wolf' Rentzsch (jon@redshed.net)
  12.  
  13.         Redistribution and use in source and binary forms, with or without modification,
  14.         are permitted provided that the following conditions are met:
  15.  
  16.         1. Redistributions of source code must retain the above copyright notice, this
  17.         list of conditions and the following disclaimer.
  18.  
  19.         2. Redistributions in binary form must reproduce the above copyright notice, this
  20.         list of conditions and the following disclaimer in the documentation and/or other
  21.         materials provided with the distribution.
  22.  
  23.         3. The names and trademarks of copyright holders may not be used in advertising
  24.         or publicity pertaining to the software without specific prior permission.
  25.  
  26.         This software is provided "as is" and any expressed or implied warranties,
  27.         including, but not limited to, the implied warranties of merchantability and
  28.         fitness for a particular purpose are disclaimed.  In no event shall the authors
  29.         or copyright holders be liable for any direct, indirect, incidental, special,
  30.         exemplary, or consequential damages (including, but not limited to, procurement
  31.         of substitute goods or services; loss of use, data, or profits; or business
  32.         interruption) however caused and on any theory of liability, whether in contract,
  33.         strict liability, or tort (including negligence or otherwise) arising in any way
  34.         out of the use of this software, even if advised of the possibility of such
  35.         damage.
  36.     
  37.     Commenter    Date                Comment
  38.     ---------    -----------------    -----------------------------------------------------
  39.     wolf        Fri, Feb 12, 1999    Created (broken out from Align.h).
  40.     wolf        Fri, Aug 6, 1999    Removed CWPro2 placement new work-around.
  41.                                     Un-inlined Align::Initialize().
  42.     wolf        Tue, Aug 10, 1999    Removed Align::Initialize() -- it was only used for
  43.                                     debugging purposes.
  44.     wolf        Mon, Dec 11, 2000    Align 1.0.
  45.     wolf        Thu, Feb 8, 2001    Align 1.0.2. CodeWarrior Pro 6 breaks an idiom:
  46.                                     implicit casting of <type>** to void**. I had to make
  47.                                     all such casts explicit.
  48.     
  49.     ************************************************************************************/
  50.  
  51. #ifndef        _Aligned_
  52. #define        _Aligned_
  53.  
  54. #include    "Align.h"
  55. #include    <new>
  56.  
  57. /*    wolf -- Fri, Aug 6, 1999: CWPro5 fixes the behavior documented below.
  58. //#include    <new.h>
  59. //    Sadly, CWPro2's placement new operator is defined as throw(),
  60. //    which causes an illegal function overloading error which I can't figure out
  61. //    how to fix. So instead of #including new.h, I just define my own placement new
  62. //    operator here.
  63.     inline
  64.     void*
  65. operator new(
  66.     size_t,
  67.     void    *location )
  68. {    return( location ); }*/
  69.  
  70. template    <class    TYPE, UInt8 ALIGNMENT = 4>
  71. class    Aligned    {
  72.     public:
  73.         Aligned()
  74.         :    ptr_( (TYPE*) this )
  75.         {    AlignX( &ptr_, ALIGNMENT );
  76.             ptr_ = new (ptr_) TYPE; }
  77.         
  78.         Aligned(
  79.             const    TYPE    &value )
  80.         :    ptr_( (TYPE*) this )
  81.         {    AlignX( (void**) &ptr_, ALIGNMENT );
  82.             ptr_ = (TYPE*) (new (ptr_) TYPE( value )); }
  83.         
  84.         ~Aligned()
  85.         {    ptr_->~TYPE(); }
  86.         
  87.         operator    TYPE()    const
  88.         {    return( *(TYPE*) ptr_ ); }
  89.         
  90.         TYPE&    operator*()
  91.         {    return( *ptr_ ); }
  92.         
  93.         TYPE*    operator->()
  94.         {    return( ptr_ ); }
  95.         
  96.         TYPE*    operator&()
  97.         {    return( ptr_ ); }
  98.     private:
  99.         UInt8    padding_[ ALIGNMENT ];
  100.         UInt8    storage_[ sizeof( TYPE ) ];
  101.         TYPE    *ptr_;
  102. };
  103.  
  104. template    <class    TYPE, UInt8 ALIGNMENT = 4>
  105. class    OldAligned    {
  106.     public:
  107.         OldAligned()
  108.         :    data_(),
  109.             ptr_( this )
  110.         {    for( short i; i < ALIGNMENT; ++i )
  111.                 padding_[ i ] = 0;
  112.             AlignX( &ptr_, ALIGNMENT ); }
  113.         
  114.         OldAligned(
  115.             const    TYPE    &value )
  116.         :    data_( value ),
  117.             ptr_( this )
  118.         {    for( short i; i < ALIGNMENT; ++i )
  119.                 padding_[ i ] = 0;
  120.             AlignX( &ptr_, ALIGNMENT ); }
  121.         
  122.         operator    TYPE()    const
  123.         {    return( *(TYPE*) ptr_ ); }
  124.         
  125.         TYPE&    operator*()
  126.         {    return( *(TYPE*) ptr_ ); }
  127.         
  128.         TYPE*    operator->()
  129.         {    return( (TYPE*) ptr_ ); }
  130.         
  131.         TYPE*    operator&()
  132.         {    return( (TYPE*) ptr_ ); }
  133.     private:
  134.         UInt8    padding_[ ALIGNMENT ];
  135.         TYPE    data_;
  136.         void    *ptr_;
  137. };
  138.  
  139. #endif    //    _Aligned_